package com.bcsq.todolistdemo.video;

import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bcsq.todolistdemo.progress.ProgressListener;
import com.bcsq.todolistdemo.util.HttpUtil;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.security.PublicKey;
import java.util.Map;

import okhttp3.Callback;
import okhttp3.Response;

/**
 * 上传视频封装类
 */
public class UploadVideo {

    private static String TAG = "uploadVideo";

    private static UploadCallBackListener uploadCallBackListener;
    private static String mPostUrl;
    private static String mFilePath;
    private static ProgressBar mPostProgress;
    private static TextView mPostText;
    private static File  file = null;


    public static void post(String postUrl,
                                  String filePath,
                                  final ProgressBar post_progress,
                                  final TextView post_text,
                            UploadCallBackListener listener)
    {
        mPostUrl = postUrl;
        mFilePath = filePath;
        mPostProgress = post_progress;
        mPostText = post_text;
        uploadCallBackListener = listener;

        upload( mPostUrl,  mFilePath,   mPostProgress,  mPostText);
    }



    /**
     *
     * @param postUrl           上传服务器路径
     * @param filePath          文件路径
     * @param post_progress     进度条
     * @param post_text         进度条显示百分数
     */
    private static void upload(String postUrl, String filePath, final ProgressBar post_progress, final TextView post_text) {

        if (null == filePath || "".equals(filePath)){
            if (uploadCallBackListener != null){
                JSONObject jsonObject = new JSONObject((Map) new Result(Result.FAIL_CODE, "文件名为空", null));
                uploadCallBackListener.onResult(jsonObject.toString());
            }
        }
        //创建上传文件
        file = new File( filePath);
        //向服务器发送数据
        HttpUtil.postFile(postUrl, new ProgressListener() {
            @Override
            public void onProgress(long currentBytes, long contentLength, boolean done) {
                int progress = (int) (currentBytes * 100 / contentLength);
                if (post_progress != null)
                    post_progress.setProgress(progress);
                if (post_text != null)
                    post_text.setText(progress + "%");
            }
        }, new Callback() {
            @Override
            public void onResponse(@NotNull okhttp3.Call call, @NotNull Response response) throws IOException {
                if (response != null) {
                    String result = response.body().string();
                    if (uploadCallBackListener != null){
                        uploadCallBackListener.onResult(result);
                        Log.i(TAG, "upload result ：" + result);
                    }
                }
            }

            @Override
            public void onFailure(@NotNull okhttp3.Call call, @NotNull IOException e) {

            }
        }, file);
    }



    /**
     * 视频上传回调
     */
    public interface UploadCallBackListener{
        void onResult(String msg);
    }

    private static class Result{
        /**
         * code 返回值
         *      1：成功
         *      2：失败
         */
        public String msg ;
        public int code;
        public Object data;
        public static int SUCCESS_CODE = 1;
        public static int FAIL_CODE = 0;

        public Result(int code, String msg, Object data){
            this.code = code;
            this.msg = msg;
            this.data = data;
        }
    }


}
